<?php
$mod = 'blank';
include("../meihe/common.php");
$title = '会员授权管理';
include './header.php';
if ($islogin2 == 1) {
} else exit("<script language='javascript'>window.location.href='./login.php';</script>");
?>
<?php
$my = isset($_GET['my']) ? $_GET['my'] : null;
if ($my == 'add') {
	if ($userrow['power'] == 1) {
		exit('<script>alert("您未开通当前功能权限！");window.location.href="./";</script>');
	}
	if ($url = $_GET['url']) {
		$url = parse_url($url);
		$url = $url['host'];
	}
	echo '<div class="row">
	<div class="col-lg-12">
		<div class="card">
			<div class="card-header">
				添加会员域名
</div>
<div class="card-body">';
	echo '<form action="./viphy.php?my=add_submit" method="POST">
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">到期时间:</label><br>
<input type="date" class="form-control" name="endtime" value="' . date('Y-m-d', strtotime('+1 year')) . '" required>
</div>
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">添加域名:</label><br>
<input type="text" class="form-control" name="domain" value="' . @$url . '" placeholder="添加域名时，请不要加http://和/"/></pre>
</div>
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">添加QQ：</label>
 <input type="text" name="qq" class="form-control" placeholder="输入添加会员QQ" autocomplete="off" lay-verType="tips" lay-verify="required"/><small>可自助更换域名验证</small>
 </div>         
<input type="submit" class="btn btn-primary btn-block" value="确定添加"></form>';
	echo '<br/><a href="./viphy.php">>>返回会员列表</a>';
	echo '</div></div></div>';
} elseif ($my == 'edit') {
	if ($userrow['power'] == 1) {
		exit('<script>alert("您未开通当前功能权限！");window.location.href="./";</script>');
	}
	echo ' <div class="row">
	<div class="col-lg-12">
		<div class="card">
			<div class="card-header">
				修改会员域名
</div>
<div class="card-body">';
	$id = $_GET['id'];
	$row = $DB->get_row("select * from meihe_list where id='$id' limit 1");
	echo '<form action="./viphy.php?my=edit_submit&id=' . $id . '" method="POST">
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">修改状态:</label><br>
 <select name="type" class="form-control border-input" default="' . $row['type'] . '">
<option value="1">开启</option><option value="2">封禁</option></select>
</div>
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">到期时间:</label><br>
<input type="date" class="form-control" name="endtime" value="' . date('Y-m-d', strtotime($row['endtime'])) . '" required>
</div>
<div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">修改域名:</label><br>
<input type="text" class="form-control" name="domain" value="' . $row['domain'] . '" required>
</div>
  <div class="form-group mb-3">
<label for="example-input-normal" style="font-weight: 500">添加QQ：</label>
 <input type="text" name="qq" value="' . $row['qq'] . '"class="form-control" placeholder="输入添加会员QQ" autocomplete="off" lay-verType="tips" lay-verify="required"/>
					</div>      
<input type="submit" class="btn btn-primary btn-block" value="确定修改"></form>
</br><a href="./viphy.php">>>返回会员列表</a><br>
</div> </div></div>';
} elseif ($my == 'edit_submit') {
	$id = $_GET['id'];
	$rows = $DB->get_row("select * from meihe_list where id='$id' limit 1");
	$domain = $_POST['domain'];
	$type = $_POST['type'];
	$qq = $_POST['qq'];
	$endtime = $_POST['endtime'];
	$rows = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}'' limit 1");
	if ($rows['type'] == 2)
		exit("<script type='text/javascript'>layer.alert('该域名为黑名单无法修改！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	if ($domain == NULL) {
		exit("<script type='text/javascript'>layer.alert('请确保每项都不为空',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	}
	if (preg_match('/^(http|https|ftp):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”])*$/', $domain)) {
		exit("<script type='text/javascript'>layer.alert('无需添加http:// 或https://',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	}
	if (!strexists($domain, ".")) {
		exit("<script type='text/javascript'>layer.alert('请您输入正确的域名！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	}
	if (strlen($qq) > 12 || !is_numeric($qq) || strlen($qq) < 5 || !preg_match('/^[0-9]+$/', $qq)) {
		exit("<script type='text/javascript'>layer.alert('请您输入正确的QQ！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	} else {
		if ($DB->query("update meihe_list set domain='$domain' ,type='$type' ,endtime='$endtime' ,qq='$qq' where id='{$id}'")) {
			exit("<script type='text/javascript'>layer.alert('修改会员完成！',{icon:6,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
		} else
			exit("<script type='text/javascript'>layer.alert('修改会员失败！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	}
} elseif ($my == 'add_submit') {
	$domain = $_POST['domain'];
	$type = $_POST['type'];
	$qq = $_POST['qq'];
	$endtime = $_POST['endtime'];
	$rows = $DB->get_row("SELECT * FROM meihe_hbmd WHERE domain='{$domain}' limit 1");
	$vipym2 = $DB->get_row("SELECT * FROM meihe_list WHERE domain='$domain' limit 1");
	$vipqq = $DB->get_row("SELECT * FROM meihe_list WHERE qq='$qq' limit 1");
	if ($rows['type'] == 2)
		exit("<script type='text/javascript'>layer.alert('该域名为黑名单无法添加！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	$row = $DB->get_row("SELECT * FROM meihe_list WHERE domain='{$domain}' limit 1");
	if ($row) {
		exit("<script type='text/javascript'>layer.alert('该域名已在系统列表中存在！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	}
	if ($domain == NULL) {
		exit("<script type='text/javascript'>layer.alert('请确保每项都不为空',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	}
	if (preg_match('/^(http|https|ftp):\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\”])*$/', $domain)) {
		exit("<script type='text/javascript'>layer.alert('无需添加http:// 或https://',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	}
	if (!strexists($domain, ".")) {
		exit("<script type='text/javascript'>layer.alert('请您输入正确的域名！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	}
	if (strlen($qq) > 12 || !is_numeric($qq) || strlen($qq) < 5 || !preg_match('/^[0-9]+$/', $qq)) {
		exit("<script type='text/javascript'>layer.alert('请您输入正确的QQ！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	} elseif ($vipym2) {
		exit("<script type='text/javascript'>layer.alert('当前域名已开通过会员',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php?mod=add'});</script>");
	} else {
		$sql = "insert into `meihe_list` (`domain`,`date`,`type`,`upzid`,`endtime`,`qq`) values ('" . $domain . "','" . $date . "','1','" . $userrow['zid'] . "','" . $endtime . "','" . $qq . "')";
		if ($DB->query($sql)) {
			exit("<script type='text/javascript'>layer.alert('添加会员完成！',{icon:6,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
		} else
			exit("<script type='text/javascript'>layer.alert('添加会员失败！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	}
} elseif ($_GET['my'] == "del") {
	$id = intval($_GET['id']);
	$sql = $DB->query("DELETE FROM meihe_list WHERE id='$id'");
	if ($sql) {
		exit("<script type='text/javascript'>layer.alert('删除会员完成！',{icon:6,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
	} else
		exit("<script type='text/javascript'>layer.alert('删除会员失败！',{icon:5,closeBtn:0},function(){window.location.href='./viphy.php'});</script>");
} elseif ($_GET['my'] == "xfhy_submit") {
	if ($userrow['power'] == 2) {
		exit('<script>alert("您当前权限！无法使用收费功能");window.location.href="./";</script>');
	}
	$domain = $_POST['domain'];
	$rows = $DB->get_row("select * from meihe_list where domain='$domain' limit 1");
	if ($rows['type'] != 1) {
		exit("<script type='text/javascript'>layer.alert('域名不存在，无法续费！',{icon:5,closeBtn:0},function(){window.location.href='./uset.php?mod=xfhy'});</script>");
	}
	if ($rows['endtime'] > date("Y-m-d"));
	$xfsj = $_POST['xfsj'];
	if ($xfsj == 4) {
		$price = $conf['xfhy4'];
		$ffktrq = 0 > 0 ? 0 : 12;
		if ($rows['endtime'] > date("Y-m-d")) $endtime = date("Y-m-d", strtotime("+               {$ffktrq} months", strtotime($rows['endtime'])));
		else $endtime = date("Y-m-d", strtotime("+ {$ffktrq} months"));
	} else if ($xfsj == 3) {
		$price = $conf['xfhy3'];
		if ($rows['endtime'] > date("Y-m-d")) $endtime = date("Y-m-d", strtotime("+               1 years", strtotime($rows['endtime'])));
		else $endtime = date("Y-m-d", strtotime('+ 1 years'));
	} else if ($xfsj == 2) {
		$price = $conf['xfhy2'];
		if ($rows['endtime'] > date("Y-m-d")) $endtime = date("Y-m-d", strtotime("+               3 months", strtotime($rows['endtime'])));
		else $endtime = date("Y-m-d", strtotime('+ 3 months'));
	} else if ($xfsj == 1) {
		$price = $conf['xfhy1'];
		if ($rows['endtime'] > date("Y-m-d")) $endtime = date("Y-m-d", strtotime("+               1 months", strtotime($rows['endtime'])));
		else $endtime = date("Y-m-d", strtotime('+ 1 months'));
	}
	if ($price > $userrow['rmb']) {
		$moneys = $price - $userrow['rmb'];
		exit("<script type='text/javascript'>layer.alert('您的余额不足，本次续费需要{$price}元，<br>还差{$moneys}元，请充值余额！',{icon:5,closeBtn:0},function(){window.location.href='./recharge.php'});</script>");
	} else {
		$sql = "update meihe_list set endtime='$endtime' where domain='{$domain}'";
		if ($DB->query($sql)) {
			$DB->query("update `meihe_user` set `rmb`=`rmb`-{$price} where `zid`='{$userrow['zid']}'");
			exit("<script type='text/javascript'>layer.alert('恭喜！成功续费到{$endtime}',{icon:6,closeBtn:0},function(){window.location.href='./uset.php?mod=hylb'});</script>");
		} else
			exit("<script type='text/javascript'>layer.alert('啊哦！续费时间失败',{icon:5,closeBtn:0},function(){window.location.href='./uset.php?mod=hylb'});</script>");
	}
} else {
	if ($userrow['power'] == 1) {
		exit('<script>alert("您未开通当前功能权限！");window.location.href="./";</script>');
	}
?>
	<?php
	if (isset($_GET['kw'])) {
		if ($_GET['type'] == 1) {
			$sql = " `domain`='{$_GET['kw']}'";
			$numrows = $DB->count("SELECT count(*) from meihe_list WHERE{$sql} and upzid='{$userrow['zid']}'");
			$con = '包含 ' . $_GET['kw'] . ' 的共有 <b>' . $numrows . '</b> 个记录';
		}
	} else {
		$numrows = $DB->count("SELECT count(*) from meihe_list WHERE 1 and upzid='{$userrow['zid']}'");
		$sql = " 1";
		$con = '';
	}
	?>
	<div class="layui-card">
		<div class="layui-card-header">您已添加 <b><?php echo $numrows; ?> </b> 个域名<br /></div>
		<div class="layui-card-body">
			<a href="?my=add" class="btn btn-primary"><i class="fa fa-plus"></i> 添加会员域名</a>
			<div style="white-space:nowrap;overflow-x: auto;">
				<table class="layui-table layuiadmin-page-table">
					<form name="form1" method="post" action="viphy.php?my=del2">
						<thead>
							<tr>
								<th>ID</th>
								<th>域名</th>
								<th>QQ</th>
								<th>状态</th>
								<th>添加/到期时间</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<?php
							$pagesize = isset($_GET['num']) ? intval($_GET['num']) : 30;
							$pages = ceil($numrows / $pagesize);
							$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
							$offset = $pagesize * ($page - 1);
							$rs = $DB->query("SELECT * FROM meihe_list WHERE{$sql} and upzid='{$userrow['zid']}' order by id desc limit $offset,$pagesize");
							while ($res = $DB->fetch($rs)) {
								echo '<tr><td>' . htmlspecialchars($res['id']) . '</td><td>' . htmlspecialchars($res['domain']) . '</td><td>' . $res['qq'] . '&nbsp;<a href="http://wpa.qq.com/msgrd?v=3&uin=' . $res['qq'] . '&site=qq&menu=yes">[<img src="../assets/icon/qqpay.ico" width="24">]</a></td><td>' . ($res['type'] == 2 ? '<font color="black">封禁</font>' : '<font color="orange">正常</font>') . '</td><td>' . $res['date'] . '<br>' . $res['endtime'] . '</td><td><a href="./viphy.php?my=edit&id=' . $res['id'] . '" class="btn btn-info btn-xs">编辑</a>&nbsp;<a href="./viphy.php?my=del&id=' . $res['id'] . '" class="btn btn-xs btn-danger " onclick="return confirm(\'你确实要删除此域名吗？\');">删除</a></td></tr>';
							}
							?>
						</tbody>
				</table>
				</form>
			</div>
			<?php
			#分页
			$pageList = new Page($numrows, $pagesize, 1, $link);
			echo $pageList->showPage();
			?>
		</div>
	</div>
	<script>
		$("select[name=\'is_curl\']").change(function() {
			if ($(this).val() == 1) {
				$("#curl_display").css("display", "inherit");
			} else {
				$("#curl_display").css("display", "none");
			}
		});

		function Addstr(id, str) {
			$("#" + id).val($("#" + id).val() + str);
		}
		var items = $("select[default]");
		for (i = 0; i < items.length; i++) {
			$(items[i]).val($(items[i]).attr("default") || 0);
		}
	</script>
	<script src="<?php echo $cdnpublic ?>layer/2.3/layer.js"></script>
	<script src="<?php echo $cdnpublic ?>clipboard.js/1.7.1/clipboard.min.js"></script>
<?php
}
include_once './bottom.php';
?>